<?php
class penugasan_followup extends ci_controller
{

	function __construct(){
		
		parent::__construct();
		$this->load->library('tank_auth');
		$this->load->library('bamer_lib');
		$this->load->model('modul/penugasan_followup_model');
		$this->load->model('user');
	//	$this->load->model('page');
	//	$this->load->model('notification');

		if (!$this->tank_auth->is_logged_in()) redirect('/auth/login/');
		$this->load->helper('bamer_helper');
		$this->lang->load('tank_auth');
		
	}

	function index(){

		//check_permission
		$this->bamer_lib->check_permission_show($this->tank_auth->get_user_id(), 'Penugasan Followup');
		//end

		$data['rows_teknisi'] = $this->penugasan_followup_model->list_teknisi();
		$data['rows_penugasan_marketing'] = $this->penugasan_followup_model->list_penugasan_marketing();
		$data['menu'] = 'penugasan';
		$data['head'] = 'layout/head';
        $data['navbar'] = 'layout/navbar';
		$data['content'] = 'modul/penugasan_followup/index';
		$this->load->view('layout/inner', $data);
	}

	function show(){
		$this->bamer_lib->check_permission_show($this->tank_auth->get_user_id(), 'Penugasan Followup');

		$data['rows_penugasan_followup'] = $this->penugasan_followup_model->show_penugasan_followup();
		$data['menu'] = 'penugasan';
		$data['head'] = 'layout/head';
        $data['navbar'] = 'layout/navbar';
		$data['content'] = 'modul/penugasan_followup/show';
		$this->load->view('layout/inner', $data);
	}

	function detail($id){
		$this->bamer_lib->check_permission_show($this->tank_auth->get_user_id(), 'Penugasan Followup');

		$data['get_penugasan_followup_detail'] = $this->penugasan_followup_model->detail_penugasan_followup($id);
		$data['kontak'] = $this->penugasan_followup_model->list_kontak();

		$data['menu'] = 'penugasan';
		$data['head'] = 'layout/head';
        $data['navbar'] = 'layout/navbar';
		$data['content'] = 'modul/penugasan_followup/detail';
		$this->load->view('layout/inner', $data);
	}
	

	function add(){
		
		//check_permission
		$row = $this->bamer_lib->check_permission_create($this->tank_auth->get_user_id(), 'Penugasan Followup');
		//end
		
		$data['menu'] = 'kelola_pegawai';
		$data['head'] = 'layout/head';
        $data['navbar'] = 'layout/navbar';
		$data['content'] = 'modul/penugasan_followup/add';
		$this->load->view('layout/inner', $data);
	}

	function edit(){
		//check_permission
		$this->bamer_lib->check_permission_update($this->tank_auth->get_user_id(), 'Penugasan Followup');
		//end

		$id = $this->uri->segment(4);

		$where = array('id_penugasan_followup' => $id);
		$where2 = array('id_global' => $id, 'kategori_kontak' => 'penugasan_followup');
		$data['penugasan_followup'] = $this->user->edit('penugasan_followup', $where);
		$data['kontak'] = $this->user->edit('kontak', $where2);
		
		
		$data['menu'] = 'kelola_pegawai';
		$data['head'] = 'layout/head';
        $data['navbar'] = 'layout/navbar';
		$data['content'] = 'modul/penugasan_followup/edit';
		$this->load->view('layout/inner', $data);
	
	}

	function create(){

		//check_permission
		$this->bamer_lib->check_permission_create($this->tank_auth->get_user_id(), 'Penugasan Followup');
		//end

		$count = count($this->input->post('teknisi'));
		for($i=1;$i<=$count;$i++){

			if ($this->input->post('teknisi')[$i] != "") {	

					$date = get_date_time();

					$data_penugasan_followup[$i] = array(
						'keterangan_penugasan_followup' => $this->input->post('keterangan_penugasan_followup')[$i],
						'id_teknisi' => $this->input->post('teknisi')[$i],
						'waktu_penugasan_followup' => $date,
						'id_penugasan_marketing' => $this->input->post('id_penugasan_marketing')[$i],
						);

					$id_penugasan_followup = $this->user->insert_get('penugasan_followup',$data_penugasan_followup[$i]);
					
					$where_penugasan_marketing[$i] = array('id_penugasan_marketing' => $this->input->post('id_penugasan_marketing')[$i]);

					$edit_penugasan_followup[$i] = array(
						'penugasan_followup' => '1',
						);
					$this->user->update('penugasan_marketing',$where_penugasan_marketing[$i], $edit_penugasan_followup[$i]);
				
					 
				
			}
			//redirect('modul/penugasan_followup/index'); 
		}
		redirect('modul/penugasan_followup/index');
	}

	function update(){
		//check_permission
		$this->bamer_lib->check_permission_update($this->tank_auth->get_user_id(), 'Penugasan Followup');
		//end

		//validation
	//	$this->form_validation->set_rules('username_penugasan_followup', 'Username', 'trim|required|xss_clean');
		$this->form_validation->set_rules('tgl_lahir_penugasan_followup', 'Tanggal Lahir', 'trim|required|xss_clean');
		$this->form_validation->set_rules('tgl_diterima_penugasan_followup', 'Tanggal Diterima', 'trim|required|xss_clean');
		$this->form_validation->set_rules('nama_penugasan_followup', 'Nama', 'trim|required|min_length[3]|xss_clean');
	//	$this->form_validation->set_rules('password_penugasan_followup', 'Password', 'trim|required|min_length[6]|xss_clean');
		$this->form_validation->set_rules('alamat_penugasan_followup', 'Alamat', 'trim|required|min_length[5]|xss_clean');
		$this->form_validation->set_rules('jenis_kelamin_penugasan_followup', 'Jenis Kelamin', 'trim|required|min_length[1]|xss_clean');

		if ($this->form_validation->run() == FALSE)
		{
			$id = $this->input->post('id_penugasan_followup');

			$where = array('id_penugasan_followup' => $id);
			$where2 = array('id_global' => $id, 'kategori_kontak' => 'penugasan_followup');
			$data['penugasan_followup'] = $this->user->edit('penugasan_followup', $where);
			$data['kontak'] = $this->user->edit('kontak', $where2);
			
			
			$data['menu'] = 'kelola_pegawai';
			$data['head'] = 'layout/head';
	        $data['navbar'] = 'layout/navbar';
			$data['content'] = 'modul/penugasan_followup/edit';
			$this->load->view('layout/inner', $data);
		}
		else
		{
			$date = get_date_time();
			$config['remove_spaces']  = TRUE;
			$config['upload_path'] = './uploads/foto/';
			$config['allowed_types'] = 'gif|jpg|png|jpeg';

			$this->load->library('upload', $config);

			if ($this->upload->do_upload()) {
				$data = array('upload_data' => $this->upload->data());
				$file = $_FILES["userfile"]["name"];
				$name = $this->input->post('name');


				$base = base_url();

				$path_to_file = './'.$this->input->post('gambar');
				

				unlink($path_to_file);
				$file = str_replace(' ', '_', $file);

				$datetime = strtotime(get_date_time());
				$data_penugasan_followup = array(
				'url_foto_penugasan_followup' => "uploads/foto/".$file,
				'username_penugasan_followup' => $this->input->post('username_penugasan_followup'),
				'nama_penugasan_followup' => $this->input->post('nama_penugasan_followup'),
				'tgl_lahir_penugasan_followup' => $this->input->post('tgl_lahir_penugasan_followup'),
				'tgl_diterima_penugasan_followup' => $this->input->post('tgl_diterima_penugasan_followup'),
				'alamat_penugasan_followup' => $this->input->post('alamat_penugasan_followup'),
				'jenis_kelamin_penugasan_followup' => $this->input->post('jenis_kelamin_penugasan_followup'),
			//	'password_penugasan_followup' => MD5($this->input->post('password_penugasan_followup')),
				);

				$url = $this->bamer_lib->url_dir();

			}else{
				$data_penugasan_followup = array(
				'username_penugasan_followup' => $this->input->post('username_penugasan_followup'),
				'nama_penugasan_followup' => $this->input->post('nama_penugasan_followup'),
				'tgl_lahir_penugasan_followup' => $this->input->post('tgl_lahir_penugasan_followup'),
				'tgl_diterima_penugasan_followup' => $this->input->post('tgl_diterima_penugasan_followup'),
				'alamat_penugasan_followup' => $this->input->post('alamat_penugasan_followup'),
				'jenis_kelamin_penugasan_followup' => $this->input->post('jenis_kelamin_penugasan_followup'),
			//	'password_penugasan_followup' => MD5($this->input->post('password_penugasan_followup')),
				);
			}

			$id_penugasan_followup = $this->input->post('id_penugasan_followup');
			$where_penugasan_followup = array('id_penugasan_followup' => $id_penugasan_followup);
			$this->user->update('penugasan_followup',$where_penugasan_followup, $data_penugasan_followup);

			
			if( !empty($_POST['delete_ids']) and is_array($_POST['delete_ids'])) {
				foreach($this->input->post('delete_ids') as $id_kon) {
					$where = array('id_kontak' => $id_kon);
					$this->user->delete('kontak', $where);
				}
			}

			$where = array('id_global' => $id_penugasan_followup, 'kategori_kontak' => 'penugasan_followup');
			$rows = $this->user->edit('kontak', $where);
			foreach($rows as $r){
				$where_kontak[$r->id_kontak] = array('id_kontak' => $r->id_kontak,'id_global' => $id_penugasan_followup, 'kategori_kontak' => 'penugasan_followup');

				$data_kontak[$r->id_kontak] = array(
					'tipe_kontak' => $this->input->post('tipe_kontak')[$r->id_kontak],
					'kontak' => $this->input->post('kontak')[$r->id_kontak],
					);

				$this->user->update('kontak', $where_kontak[$r->id_kontak], $data_kontak[$r->id_kontak]);
			}

			if(!empty($_POST['kontak'])){

				$banyak_kontak = $this->input->post('kontak');
				$tipe_kontak = $this->input->post('tipe_kontak');

				$count_banyak_kontak = count($banyak_kontak);

				for($i='2'; $i <= $count_banyak_kontak; $i++){
					if($banyak_kontak[$i]==""){
						$kosong[$i]='1';
					}
				}
				
				$count_kontak_kosong = count($kosong);

				$count = $count_banyak_kontak-$count_kontak_kosong;
				for($i='2'; $i <= $count; $i++){
					$data_kontak[$i] = array(
						'kategori_kontak' => "penugasan_followup",
						'tipe_kontak' => $tipe_kontak[$i],
						'kontak' => $banyak_kontak[$i],
						'id_global' => $id_penugasan_followup,
						);

					$this->user->insert('kontak', $data_kontak[$i]);
				}

			}

			redirect('modul/penugasan_followup/index');
		}

	}

	function delete(){
		//check_permission
		$this->bamer_lib->check_permission_delete($this->tank_auth->get_user_id(), 'Penugasan Followup');
		//end

		$id_penugasan_followup = $this->uri->segment(4);

		$where = array('id_penugasan_followup' => $id_penugasan_followup);
		$rows = $this->user->edit('penugasan_followup', $where);

		foreach($rows as $row){
			$base = base_url();

			$path_to_file = './'.$row->url_foto_penugasan_followup;
	
			unlink($path_to_file); 
		}

		$where = array('id_penugasan_followup' => $id_penugasan_followup);
		$this->user->delete('penugasan_followup', $where);
		$where = array('id_global' => $id_penugasan_followup, 'kategori_kontak' => 'penugasan_followup');
		$this->user->delete('kontak', $where);
		
		redirect('modul/penugasan_followup/index');
	}

	function search_index(){
		//check_permission
		$this->bamer_lib->check_permission_show($this->tank_auth->get_user_id(), 'Penugasan Followup');
		//end
		$keyword = $this->input->post('cari');

		if($keyword == 'Cari Perusahaan...') $keyword = "";
		else $keyword = $keyword;

		$data['rows_teknisi'] = $this->penugasan_followup_model->list_teknisi();
		$data['rows_penugasan_marketing'] = $this->penugasan_followup_model->list_penugasan_marketing_search($keyword);
		$data['menu'] = 'penugasan';
		$data['head'] = 'layout/head';
        $data['navbar'] = 'layout/navbar';
		$data['content'] = 'modul/penugasan_followup/index';
		$this->load->view('layout/inner', $data);
	}

	function search_list_penugasan(){
		//check_permission
		$this->bamer_lib->check_permission_show($this->tank_auth->get_user_id(), 'Penugasan Followup');
		//end
		$keyword = $this->input->post('cari');

		if($keyword == 'Cari Perusahaan...') $keyword = "";
		else $keyword = $keyword;

		$data['rows_penugasan_followup'] = $this->penugasan_followup_model->show_penugasan_followup_search($keyword);
		$data['menu'] = 'penugasan';
		$data['head'] = 'layout/head';
        $data['navbar'] = 'layout/navbar';
		$data['content'] = 'modul/penugasan_followup/show';
		$this->load->view('layout/inner', $data);
	}
}
?>